package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysDeptMapper;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysDeptExample;
import com.sofwin.service.DeptService;
import com.sofwin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2023/5/10 15:07
 * @email 115176513@qq.com
 * @description: TODO
 */

public class DeptServiceImpl implements DeptService {

    @Autowired
    private SysDeptMapper mapper;

    @Override
    public boolean saveOrUpdateDept(SysDept dept) {
        if(dept.getId()==null){
            dept.setGmtCreate(DateUtil.getNow());
            return mapper.insertSelective(dept)==1?true:false;
        }
        dept.setGmtModified(DateUtil.getNow());
        return mapper.updateByPrimaryKeySelective(dept)==1?true:false;
    }

    @Override
    public SysDept queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeById(int[] ids) {
        int num=0;
        for(int id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }
    @Override
    public boolean removeById(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }
    @Override
    public List<SysDept> queryDepts(SysDept dept) {
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(dept.getDeptName())){
            criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
        }
        if(dept.getStatus()!=null && dept.getStatus()!=-1){
            criteria.andStatusEqualTo(dept.getStatus());
        }
        // 所有的查询都必须排序
        example.setOrderByClause("id desc");
        return mapper.selectByExample(example);
    }

    @Override
    public PageInfo<SysDept> queryByPage(SysDept dept, Integer pageNumber, Integer pageSize) {
        PageInfo<SysDept> page = PageHelper.startPage(pageNumber, pageSize).doSelectPageInfo(() -> {
            SysDeptExample example = new SysDeptExample();
            SysDeptExample.Criteria criteria = example.createCriteria();
            if(!StringUtils.isEmpty(dept.getDeptName())){
                criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
            }
            // 所有的查询都必须排序
            example.setOrderByClause("id desc");
            mapper.selectByExample(example);
        });
        return page;
    }
}
